package drds.data_propagate.binlog;


public class BinlogPosition implements Cloneable, Comparable<BinlogPosition> {

    /* binlog file's name */
    protected String fileName;

    /* position in file */
    protected long position;

    /**
     * @param fileName file name for binlog files: mysql-bin.000001
     */
    public BinlogPosition(String fileName) {
        this.fileName = fileName;
        this.position = 0L;
    }

    public BinlogPosition(String fileName, final long position) {
        this.fileName = fileName;
        this.position = position;
    }


    public final String getFileName() {
        return fileName;
    }

    public final long getPosition() {
        return position;
    }

    /* Clone binlog position without CloneNotSupportedException */
    public BinlogPosition clone() {
        try {
            return (BinlogPosition) super.clone();
        } catch (CloneNotSupportedException e) {
            // Never happend
            return null;
        }
    }


    public int compareTo(BinlogPosition o) {
        final int val = fileName.compareTo(o.fileName);

        if (val == 0) {
            return (int) (position - o.position);
        }
        return val;
    }

    public boolean equals(Object obj) {
        if (obj instanceof BinlogPosition) {
            BinlogPosition pos = ((BinlogPosition) obj);
            return fileName.equals(pos.fileName) && (this.position == pos.position);
        }
        return false;
    }


    public String toString() {
        return fileName + ':' + position;
    }
}
